package com.ruoyi.livedemo.controller;

import com.ruoyi.livedemo.common.ApiResponse;
import com.ruoyi.livedemo.context.CurrentUserHolder;
import com.ruoyi.livedemo.dto.PagedResult;
import com.ruoyi.livedemo.dto.RoutineSlotRequest;
import com.ruoyi.livedemo.entity.RoutineSlot;
import com.ruoyi.livedemo.service.RoutineService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/routines")
public class RoutineController {

    private final RoutineService routineService;

    @GetMapping
    public ApiResponse<PagedResult<RoutineSlot>> list(@RequestParam(defaultValue = "0") int page,
                                                      @RequestParam(defaultValue = "10") int size) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok(routineService.list(userId, page, size));
    }

    @PostMapping
    public ApiResponse<RoutineSlot> create(@Valid @RequestBody RoutineSlotRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("新增作息成功", routineService.create(userId, request));
    }

    @PutMapping("/{id}")
    public ApiResponse<RoutineSlot> update(@PathVariable Long id, @Valid @RequestBody RoutineSlotRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("作息已更新", routineService.update(userId, id, request));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        Long userId = CurrentUserHolder.getUserId();
        routineService.delete(userId, id);
        return ApiResponse.ok("作息已删除", null);
    }
}
